package com.ljl.util;

import com.ljl.domain.XmlData;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ljl
 * @version 1.0
 * @date 2022/10/9
 */
public class XmlResolveTool {

    /**
     * 将xml文件解析成xmlData类
     * @param xmlFile xml文件
     * @return /
     */
    public static XmlData resolveXml(String xmlFile) {
        File file1 = new File(xmlFile);
        if (!file1.exists()) {
            throw new RuntimeException("参数错误，找不到文件，请检查xml文件是否存在");
        }
        try {
            SAXReader saxReader = new SAXReader();
            Document read = saxReader.read(file1);
            Element rootElement = read.getRootElement();
            return resolve(rootElement, "", 1);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将rootElement处理成xmlData类
     * @param rootElement /
     * @param ancestorNames 祖级名称
     * @param level 等级
     * @return /
     */
    private static XmlData resolve(Element rootElement, String ancestorNames, Integer level) {
        //新建xml数据对象
        XmlData xmlData = new XmlData();
        xmlData.setLevel(level);
        xmlData.setName(rootElement.getName());
        xmlData.setAncestorNames(ancestorNames);
        //处理属性
        Map<String, String> properties = new HashMap<>();
        List<Attribute> attributes = rootElement.attributes();
        attributes.forEach(item -> {
            String name = item.getName();
            String value = item.getValue();
            properties.put(name, value);
        });
        xmlData.setProperties(properties);
        //处理子节点
        List<XmlData> children = new ArrayList<>();
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            //拼接父级名称
            String ancestorName = (xmlData.getAncestorNames() == null || "".equals(xmlData.getAncestorNames())) ? "" : xmlData.getAncestorNames() + ",";
            ancestorName += xmlData.getName();
            XmlData resolve = resolve(element, ancestorName, level + 1);
            children.add(resolve);
        }
        xmlData.setChildren(children);
        return xmlData;
    }

    public static void collectionBottomXmlData(XmlData data, List<XmlData> out) {
        List<XmlData> children = data.getChildren();
        if (children == null || children.size() == 0) {
            out.add(data);
        }else {
            for (XmlData child : children) {
                collectionBottomXmlData(child, out);
            }
        }
    }

    /**
     * 将集合中的xmlData根据标签名整理成map
     * @param list /
     * @return /
     */
    public static Map<String, List<XmlData>> collectionXmlDataByName(List<XmlData> list) {
        if (list != null) {
            Map<String, List<XmlData>> map = new HashMap<>();
            for (XmlData data : list) {
                String key = (data.getAncestorNames() == null) || ("".equals(data.getAncestorNames())) ? "" : data.getAncestorNames() + ",";
                key += data.getName();
                List<XmlData> temp = map.get(key);
                if (temp == null) {
                    List<XmlData> a = new ArrayList<>();
                    map.put(key, a);
                    temp = map.get(key);
                }
                temp.add(data);
            }
            return map;
        }else {
            return new HashMap<>();
        }
    }
}
